/*******************************************************************************
	- Regression Analysis (Robustness checks)
*******************************************************************************/

********************************************************************************
* initial settings
********************************************************************************

version 14.2
clear 
drop _all
set more off

*----------------------------------

// set your own local path where you put the JAERE_Submission folder
if c(username) == "zhenx" {
	global LOCALPATH "C:/Users/zhenx/Dropbox/Research/Temperature and Athletes/temp_acclimatization/Code" 
}
global PATH "$LOCALPATH/JAERE_Submission"

********************************************************************************
* list of robustness checks
********************************************************************************
/*
	1: exclude covariates (Table A9, Figure A4a, A6a)
	2: replace dew point with relative humidity (Table A10, Figure A4b, A6b)
	3: add team-venue FE （Table A12, Figure A4c, A6c)
	4: alternative clustered SE （Figure A5a, Figure A6d)
	5: no weather interpolation （Table A14, Figure A5c, A7c)	
	6: two-week pre-exposure （Table A11, A7b)
	7: use maximum temperature （Table A13, Figure A5b, A7a)
*/

*** choose the robustness check
global robustchecks 1 // 1 2 3 4 5 6 7

foreach robust_case in $robustchecks {

	********************************************************************************
	* data preparation
	********************************************************************************

	*** load data set 
		use "$PATH/Data/TF_Data.dta", clear

	*** drop training temperature lower than 40F to mitigate noise
		/*avoid the case where athletes might get trained indoors during extreme cold weathers*/
		drop if Team_tavg <= 40 

	*** drop the observations that have missing values on key variables
		drop if Meet_tavg == . | Team_tavg == . | Meet_preciptotal ==. | Meet_ozone == .

	*** settings for different robustness checks
		* exclude covariates
		if `robust_case' == 1 {
			global T_tile1 = 50 
			global T_tile2 = 60
			global weather_control 
			global absorb_var athlete season_week event_code##(venue home1) year_code
			global cluster_se meet_code athlete
			cd "$PATH/Results/Regressions/Robustness/Exclude Covariate"
		}

		* replace dew point with relative humidity
		if `robust_case' == 2 {
			global T_tile1 = 50 
			global T_tile2 = 60
			global weather_control wind Meet_preciptotal Meet_ozone travel_dist 
			global absorb_var athlete season_week event_code##(venue home1) year_code i.RH_bin
			global cluster_se meet_code athlete
			cd "$PATH/Results/Regressions/Robustness/Relative Humidity"
		}

		* add team-venue FE
		if `robust_case' == 3 {
			global T_tile1 = 50 
			global T_tile2 = 60
			global weather_control wind Meet_preciptotal Meet_ozone travel_dist 
			global absorb_var athlete season_week event_code##(venue home1) year_code i.Meet_dew_bin team_code#venue
			global cluster_se meet_code athlete
			cd "$PATH/Results/Regressions/Robustness/Add Team-Venue FE"
		}

		* alternative clustered SE 
		if `robust_case' == 4 {
			global T_tile1 = 50 
			global T_tile2 = 60
			global weather_control wind Meet_preciptotal Meet_ozone travel_dist 
			global absorb_var athlete season_week event_code##(venue home1) year_code i.Meet_dew_bin 
			global cluster_se team_code season_week
			cd "$PATH/Results/Regressions/Robustness/Alternative Clustered SE"
		}

		* no weather interpolation 	
		if `robust_case' == 5 {
			replace Team_tavg 	  	 = TeamNI_tavg_7
			replace Team_hotter60 	 = TeamNI_hotter60_7
			replace Meet_tavg 		 = MeetNI_tavg
			replace Meet_temp_bin    = MeetNI_temp_bin
			replace Meet_dew_bin     = MeetNI_dew_bin
			replace Meet_preciptotal = MeetNI_preciptotal
			replace temp_diff = Team_tavg - Meet_tavg
			replace hot = cond(Team_tavg>=60,1,0)	

			global T_tile1 = 50 
			global T_tile2 = 60
			global weather_control wind Meet_preciptotal Meet_ozone travel_dist 
			global absorb_var athlete season_week event_code##(venue home1) year_code i.Meet_dew_bin 
			global cluster_se meet_code athlete
			cd "$PATH/Results/Regressions/Robustness/No Weather Interpolation"
		}

		* two-week pre-exposure
		if `robust_case' == 6 {
			replace Team_tavg 	  = Team_tavg_14
			replace Team_hotter60 = Team_hotter60_14
			replace temp_diff = Team_tavg - Meet_tavg
			replace hot = cond(Team_tavg>=60,1,0)

			global T_tile1 = 50 
			global T_tile2 = 60
			global weather_control wind Meet_preciptotal Meet_ozone travel_dist 
			global absorb_var athlete season_week event_code##(venue home1) year_code i.Meet_dew_bin 
			global cluster_se meet_code athlete
			cd "$PATH/Results/Regressions/Robustness/Two Week Pre-Exposure"
		} 

		* use maximum temperature
		if `robust_case' == 7 {
			replace Team_tavg = Team_tmax
			replace Team_hotter60 = Team_hottermax60_7
			replace Meet_tavg = Meet_tmax
			replace Meet_temp_bin = Meet_tmax_bin
			replace temp_diff = Team_tavg - Meet_tavg
			replace hot = cond(Team_tavg>=70,1,0)	

			global T_tile1 = 60 
			global T_tile2 = 70
			global weather_control wind Meet_preciptotal Meet_ozone travel_dist 
			global absorb_var athlete season_week event_code##(venue home1) year_code i.Meet_dew_bin 
			global cluster_se meet_code athlete
			cd "$PATH/Results/Regressions/Robustness/Maximum Temperature"
		}

	********************************************************************************
	* nonlinear relation between temperature and performance by event type
	********************************************************************************
	if `robust_case' != 6 {
		foreach event in Endurance Sprint Strength {
			preserve
				keep if event_type == "`event'"
				reghdfe norm_result ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se) baselevels 
				outreg2 using Temperature_Effect_by_Event_Type.xls, excel ctitle("`event'") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 
			restore
		}
	}

	********************************************************************************
	* adaptation from one-week exposure to warm temperatures
	********************************************************************************

	preserve
		keep if event_type == "Endurance"
		
		* (1) temperature difference
		reghdfe norm_result ib60.Meet_temp_bin c.temp_diff#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Temp_Diff.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (2) # days > 60 F in one week
		reghdfe norm_result ib60.Meet_temp_bin c.Team_hotter60#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Hotter_Days.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (3) mean training temperature
		reghdfe norm_result ib60.Meet_temp_bin c.Team_tavg#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Temp_Tavg.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (4) hottest 3rd of pre-exposure
		reghdfe norm_result ib60.Meet_temp_bin ib60.Meet_temp_bin#c.hot $weather_control if Team_tavg<$T_tile1 | Team_tavg>$T_tile2, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Hot_Dummy.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	restore


	********************************************************************************
	* nonlinear relation between temperature and performance
	********************************************************************************

	preserve

		keep if event_type == "Endurance" 
		keep if Team_tavg<$T_tile1 | Team_tavg>$T_tile2

		* quadratic
		reghdfe norm_result c.Meet_tavg##c.Meet_tavg (c.Meet_tavg##c.Meet_tavg)#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se) 
		estimates save "Endurance_Quadratic", replace

		* cubic
		reghdfe norm_result c.Meet_tavg##c.Meet_tavg##c.Meet_tavg (c.Meet_tavg##c.Meet_tavg##c.Meet_tavg)#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		estimates save "Endurance_Cubic", replace

		* linear spline - 2 knots: 50F and 60F
		mkspline meet_linspl1 $T_tile1 meet_linspl2 $T_tile2 meet_linspl3  = Meet_tavg, di
		reghdfe norm_result meet_linspl* c.meet_linspl*#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		estimates save "Endurance_Linear_Spline", replace

		* cubic spline
		if `robust_case' != 7 {
			mkspline2 meet_cubspl= Meet_tavg, cubic di knots(49.6 56.7 63.8)
		}
		if `robust_case' == 7 {
			mkspline2 meet_cubspl= Meet_tmax, cubic di knots(59 67 75)
		}
		reghdfe norm_result meet_cubspl* c.meet_cubspl*#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		estimates save "Endurance_Cubic_Spline", replace

	restore

}
